package his.controller;

import com.alibaba.fastjson.JSONObject;
import his.entity.ConstantType;
import his.entity.Department;
import his.service.DepartmentService;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/dept/")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    @PostMapping("add")
    @ResponseBody
    public CommonResponse new_department(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String deptCode=(String) jsonObject.get("deptCode");
        String deptName=(String) jsonObject.get("deptName");
        int deptCategoryID=Integer.parseInt((String) jsonObject.get("deptCategoryID"));
        int deptType= Integer.parseInt((String) jsonObject.get("deptType"));
        return departmentService.new_department(deptCode,deptName,deptCategoryID,deptType);

    }


    @GetMapping("all")
    @ResponseBody
    public CommonResponse<List<Department>> getall_departments()
    {
        return departmentService.getall_departments();

    }

    @GetMapping("search/{keyword}")
    @ResponseBody
    public CommonResponse<List<Department>> get_departments_bycodename(@PathVariable String keyword)
    {
        return departmentService.get_departments_bycodename(keyword);

    }

    @PostMapping("convert_id_to_name/{deptid}")
    @ResponseBody
    public CommonResponse<String> convert_id_to_name(@PathVariable int deptid)
    {
        return departmentService.convert_id_to_name(deptid);

    }

    @DeleteMapping("del/{dept_category_id}")
    @ResponseBody
    public CommonResponse<List<Department>> delete_departments_byid(@PathVariable int dept_category_id)
    {
        return departmentService.delete_departments_byid(dept_category_id);

    }

    @PostMapping("modify/{dept_category_id}")
    @ResponseBody
    public CommonResponse<List<Department>> update_departments_byid(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        int dept_category_id=(int) jsonObject.get("dept_category_id");
        String deptCode=(String) jsonObject.get("deptCode");
        String deptName=(String) jsonObject.get("deptName");
        int deptCategoryID=(int) jsonObject.get("deptCategoryID");
        int deptType=(int) jsonObject.get("deptType");
        return departmentService.update_departments_byid(dept_category_id,deptCode,deptName,deptCategoryID,deptType);

    }



}
